{{Extend "db/index"}}
{{Block "title"}}{{"外键"|T}}: {{Stored.table}} - {{Super}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li><a href="{{dbMgrURL "listTable" Stored.dbName}}" data-pjax="#pcont" data-keepjs="true">{{"数据库"|T}}: {{Stored.dbName}}</a></li>
<li><a href="{{dbMgrURL "viewTable" Stored.dbName Stored.table}}" data-pjax="#pcont" data-keepjs="true">{{"表"|T}}: {{Stored.table}}</a></li>
<li class="active">{{"外键"|T}}</li>
{{/Block}}
{{Block "main"}}
{{$driver := Form "driver"}}
{{$supportSQL := Stored.supportSQL}}
<div class="row">
    <div class="col-md-12">
        {{Include "db/mysql/results"}}
        <div class="block-flat">
          <div class="header">							
            <h3>
                {{"外键"|T}}: {{Stored.table}}
            </h3>
          </div>
          <div class="content">
              <form class="form-horizontal group-border-dashed" method="POST" action="">

              <div class="form-group">
                <label class="col-sm-2 control-label">{{"目标表"|T}}</label>
				<div class="col-sm-9">
				  <select name="foreign_table" class="form-control" onchange="onselectForeignTable(this)">
                    <option value=""></option>
                    {{$foreign := Stored.foreign}}
                    {{range $k2,$v2 := Stored.referencable}}
                    <option value="{{$v2}}"{{if eq $foreign.Table $v2}} selected{{end}}>{{$v2}}</option>
                    {{end}}
                   </select>
				</div>
			</div>
              <div class="form-group">
				<div class="col-sm-9 col-sm-offset-2">
              <table>
                  <thead>
                      <th><strong>{{"目标字段"|T}}</strong></th>
                      <th><strong>{{"源字段"|T}}</strong>({{"本表字段"|T}})</th>
                      <!-- <th style="width:40px"></th> -->
                  </thead>
                  <tbody>
                      {{range $k,$v := $foreign.Source}}
                      {{$target := index $foreign.Target $k}}
                      <tr>
                          <td>
                              <select name="target[]" class="form-control" onchange="onselectTarget(this)">
                                <option value=""></option>
                                {{range $k2,$v2 := Stored.target}}
                                <option value="{{$v2}}"{{if eq $target $v2}} selected{{end}}>{{$v2}}</option>
                                {{end}}
                              </select>
                          </td>
                          <td>
                              <select name="source[]" class="form-control" onchange="onselectSource(this)">
                                <option value=""></option>
                                {{range $k2,$v2 := Stored.source}}
                                <option value="{{$v2}}"{{if eq $v $v2}} selected{{end}}>{{$v2}}</option>
                                {{end}}
                              </select>
                          </td>
                          <!-- <td><a href="javascript:;" onclick="delRow(this)" class="label label-danger">
                              <i class="fa fa-times"></i>
                              </a></td> -->
                      </tr>
                      {{end}}
                  </tbody>
              </table>
              </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">ON DELETE</label>
				<div class="col-sm-9">
				  <select name="on_delete" class="form-control">
                    <option value=""></option>
                    {{range $k2,$v2 := Stored.onActions}}
                    <option value="{{$v2}}"{{if eq $foreign.OnDelete $v2}} selected{{end}}>{{$v2}}</option>
                    {{end}}
                   </select>
				</div>
			</div>
              <div class="form-group">
                <label class="col-sm-2 control-label">ON UPDATE</label>
				<div class="col-sm-9">
				  <select name="on_update" class="form-control">
                    <option value=""></option>
                    {{range $k2,$v2 := Stored.onActions}}
                    <option value="{{$v2}}"{{if eq $foreign.OnUpdate $v2}} selected{{end}}>{{$v2}}</option>
                    {{end}}
                   </select>
				</div>
			</div>
              <div class="form-group form-submit-group">
				<div class="col-sm-9 col-sm-offset-2">
				  <button type="submit" class="btn btn-primary btn-lg"><i class="fa fa-save"></i> {{"保存"|T}}</button>
				  <a class="btn btn-default btn-lg" href="{{dbMgrURL "viewTable" Stored.dbName Stored.table}}" data-pjax="#pcont" data-keepjs="true"><i class="fa fa-reply"></i> {{"返回"|T}}</a>
				</div>
			</div>
              </form>
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
{{/Block}}
{{Block "footer"}}
{{Super}}
<script>
function onselectForeignTable(obj){
    window.location='{{dbMgrURL "foreign" Stored.dbName Stored.table}}&name={{Form "name"}}&foreign_table='+$(obj).val();
}
function onselectSource(obj){
    onselectTarget(obj);
}
function onselectTarget(obj){
    if($(obj).val()=='')return;
    var currentTR=$(obj).parent().parent();
    var nextTR=currentTR.next('tr');
    if(nextTR.length<1){
        var clone=currentTR.clone();
        clone.find('select').children('option:selected').prop('selected',false);
        currentTR.after(clone);
    }
}
function delRow(obj){
    $(obj).parent().parent().remove();
}
</script>
{{/Block}}